﻿/// Name - MyHealth/Registration.cs
/// Description - Allows the user to sign up to use My Health system
/// Programmer - Dan Yatzeck
/// Date coded - 10/26/2011
/// Arguments - None
/// Variables -
///     _presenter - Object linking to parent Presenter
/// Files accessed by this code artifact - None
/// Files changed by this code artifact - None
/// I/O -
///     Output - Web page
/// Error Handling - None
/// Modifications -
///     1.0 - Initial coding
/// Known Faults -
///     1.0 - None known

using System;
using Microsoft.Practices.ObjectBuilder;
using Common;

namespace CSC440TeamOne.MyHealth.Views
{
    /// <summary>
    /// Registration page
    /// </summary>
	public partial class Registration : Microsoft.Practices.CompositeWeb.Web.UI.Page, IRegistrationView
	{
		private RegistrationPresenter _presenter;

        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

        /// <summary>
        /// When this object is instantiated, create the presenter link
        /// </summary>
		[CreateNew]
		public RegistrationPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

        /// <summary>
        /// Register button click event
        /// Creates a user object, adds it to the Context.Items, redirects to processregistration
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void register_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // Create user object from fields on screen
                string firstname = txtFirstName.Text;
                string lastname = txtLastName.Text;
                string emailaddress = txtEmailAddress.Text;
                DateTime birthdate = DateTime.Parse(txtBirthdate.Text);
                string gender = radioGender.SelectedValue;
                string ethnicity = (cboEthnicity.SelectedValue != "Other") ? cboEthnicity.SelectedValue : txtEthnicityOther.Text;
                string zipcode = txtZipcode.Text;
                string country = txtCountry.Text;

                User user = new User(firstname, 
                                     lastname,
                                     emailaddress,
                                     birthdate,
                                     gender,
                                     ethnicity,
                                     zipcode,
                                     country);
                
                // Save object to Context.Items
                Context.Items["userinfo"] = user;

                // Redirect
                Server.Transfer("ProcessRegistration.aspx");
            }
        }

}
}

